package com.java.order.orderweb.service.impl;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.java.base.pojo.EasyUIResult;
import com.java.order.orderweb.bean.CQZGResult;
import com.java.order.orderweb.dao.IOder;
import com.java.order.orderweb.mapper.OrderMapper;
import com.java.order.orderweb.pojo.Order;
import com.java.order.orderweb.pojo.ResultMsg;
import com.java.order.orderweb.service.OrderService;
import com.java.order.orderweb.utils.ValidateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author TriggerWu
 * @since 2018-09-13
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {
    private static final ObjectMapper objectMapper = new ObjectMapper();
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private IOder orderDao;
    /*
     *  创建订单
     *
     * @author Tigger Wu
     * @date 2018/9/21 9:52
     * @param [json]
     * @return com.java.order.orderweb.bean.CQZGResult
     */
    public CQZGResult createOrder(String json) {
        Order order = null;
        try {
            order = objectMapper.readValue(json, Order.class);
            // 校验Order对象
            ValidateUtil.validate(order);
        } catch (Exception e) {
            return CQZGResult.build(400, "请求参数有误!");
        }

        try {
            // 生成订单ID，规则为：userid+当前时间戳
            String orderId = order.getUserId() + "" + System.currentTimeMillis();
            order.setOrderId(orderId);

            // 设置订单的初始状态为未付款
            order.setStatus(1);

            // 设置订单的创建时间
            order.setCreateTime(new Date());
            order.setUpdateTime(order.getCreateTime());

            // 设置买家评价状态，初始为未评价
            order.setBuyerRate(0);

            // 持久化order对象
            orderDao.createOrder(order);

            //发送消息到RabbitMQ
//            Map<String, Object> msg = new HashMap<String, Object>(3);
//            msg.put("userId", order.getUserId());
//            msg.put("orderId", order.getOrderId());
//            List<Long> itemIds = new ArrayList<Long>(order.getOrderItems().size());
//            for (OrderItem orderItem : order.getOrderItems()) {
//                itemIds.add(orderItem.getItemId());
//            }
//            msg.put("itemIds", itemIds);
//            this.rabbitTemplate.convertAndSend(objectMapper.writeValueAsString(msg));

            return CQZGResult.ok(orderId);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return CQZGResult.build(400, "保存订单失败!");
    }
    /*
     *  查询订单
     *
     * @author Tigger Wu
     * @date 2018/9/21 9:53
     * @param [orderId]
     * @return com.java.order.orderweb.pojo.Order
     */
    public Order queryOrderById(String orderId) {

        Order order = orderMapper.queryByID(orderId);
        return order;
    }
    /*
     *  分页查询信息
     * 
     * @author Tigger Wu
     * @date 2018/9/21 11:20
     * @param [orderid, page, rows]
     * @return com.java.base.pojo.EasyUIResult
     */
    public EasyUIResult queryListByOrderId(String buyerNick, Integer page, Integer rows){
        Page<Order> contentPage = new Page<Order>(page,rows);
        List<Order> orders = orderMapper.queryList(contentPage,buyerNick);
        contentPage.setRecords(orders);
        EasyUIResult ez = new EasyUIResult();
        ez.setRows(contentPage.getRecords());
        ez.setTotal(contentPage.getTotal());
        return  ez;
    }
    /*
     *  修改订单状态
     * 
     * @author Tigger Wu
     * @date 2018/9/21 15:13
     * @param []
     * @return void
     */
    public ResultMsg changeOrderStatus(String json) throws IOException {
        Order order = null;
        try {
            order = objectMapper.readValue(json,Order.class);
        }catch (Exception e){
            e.printStackTrace();
            return new ResultMsg("404","请求参数错误！");
        }
        return orderDao.changeOrderStatus(order);
    }
}
